package com.anthony.app.module;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.anthony.app.R;
import com.anthony.app.common.base.AbsBaseActivity;
import com.anthony.app.common.injection.component.ActivityComponent;
import com.anthony.app.common.utils.ToastUtils;
import com.anthony.app.common.widgets.ViewDisplay;
import com.anthony.app.module.film.FilmFragment;
import com.mikepenz.fontawesome_typeface_library.FontAwesome;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * ============================================================
 * 版权： xx有限公司 版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/21 21:06.
 * 描述：主页面
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */

public class MainActivity extends AbsBaseActivity implements Drawer.OnDrawerItemClickListener {

    @Inject
    ViewDisplay mViewDisplay;
    @Inject
    ToastUtils toastUtils;

    private AccountHeader headerResult = null;
    private Drawer result = null;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;


    @Override
    protected int getContentViewID() {
        return R.layout.activity_main;
    }


    @Override
    protected void initViewsAndEvents() {
        setupToolbar();
        setupDrawer();
        setupViewPager();
    }


    private void setupViewPager() {
        SimplePagerAdapter adapter = new SimplePagerAdapter(getSupportFragmentManager());
        adapter.addFrag(FilmFragment.newInstance(), "RxIdentifier");
        adapter.addFrag(FilmFragment.newInstance(), "RxPager");
        adapter.addFrag(FilmFragment.newInstance(), "No Page");
        mViewPager.setAdapter(adapter);

//        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, RxApp.appResources().getDisplayMetrics());
//        mViewPager.setPageMargin(pageMargin);
    }

    private void setupToolbar() {
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            //隐藏标题
            getSupportActionBar().setDisplayShowTitleEnabled(false);
//            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//            getSupportActionBar().setTitle(R.string.app_name);
        }
    }

    private void setupDrawer() {
        // Create a few sample profile
        // NOTE you have to define the loader logic too. See the CustomApplication for more details
        final IProfile profile = new ProfileDrawerItem().withName("Smartydroid")
                .withEmail("smartydroid@gmail.com")
                .withIcon("https://avatars2.githubusercontent.com/u/13810934?v=3&s=460");

        // Create the AccountHeader
        headerResult = new AccountHeaderBuilder().withActivity(this)
                .withHeaderBackground(R.drawable.header)
                .addProfiles(profile)
                .build();

        //Create the drawer
        result = new DrawerBuilder().withActivity(this)
                .withToolbar(mToolbar)
                .withHasStableIds(true)
                .withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
                .addDrawerItems(
                        new PrimaryDrawerItem().withName("Feeds").withIcon(FontAwesome.Icon.faw_android).withTag(TAG_FEEDS),
                        new PrimaryDrawerItem().withName("Login").withIcon(FontAwesome.Icon.faw_adjust).withTag(TAG_LOGIN),
                        new PrimaryDrawerItem().withName("SimpleHud").withIcon(FontAwesome.Icon.faw_adjust).withTag(TAG_SIMPLE_HUD),
                        new PrimaryDrawerItem().withName("Content").withIcon(FontAwesome.Icon.faw_connectdevelop).withTag(TAG_CONTENT)
                )
                .addStickyDrawerItems(
                        new PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog),
                        new PrimaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github)
                )
                .withOnDrawerItemClickListener(this)
                .withShowDrawerOnFirstLaunch(false)//抽屉默认关闭
                .build();
    }

    private static final String TAG_FEEDS = "feeds";
    private static final String TAG_LOGIN = "login";
    private static final String TAG_SIMPLE_HUD = "SimpleHud";
    private static final String TAG_CONTENT = "ContentDemo";

    @Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
        if (TAG_LOGIN.equals(drawerItem.getTag())) {
//            startActivity(new Intent(MainActivity.this, LoginActivity.class));
            return true;
        }
        if (TAG_SIMPLE_HUD.equals(drawerItem.getTag())) {
//            startActivity(new Intent(MainActivity.this, SimpleHudActivity.class));
            return true;
        }
        if (TAG_CONTENT.equals(drawerItem.getTag())) {
//            startActivity(new Intent(MainActivity.this, ContentActivity.class));
            return true;
        }
        return false;
    }

    @Override protected void onSaveInstanceState(Bundle outState) {
        //add the values which need to be saved from the drawer to the bundle
        outState = result.saveInstanceState(outState);
        //add the values which need to be saved from the accountHeader to the bundle
        outState = headerResult.saveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override public void onBackPressed() {
        //handle the back press :D close the drawer first and if the drawer is closed close the activity
        if (result != null && result.isDrawerOpen()) {
            result.closeDrawer();
        } else {
            super.onBackPressed();
        }
    }

    static class SimplePagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitles = new ArrayList<>();

        public SimplePagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }


    @Override
    protected void injectDagger(ActivityComponent activityComponent) {
        super.injectDagger(activityComponent);
        activityComponent.inject(this);
    }
}
